<template>
  <div class="a-cardui">
    <div class="a-cardui-header">
      <h2 class="headline">{{ title }}</h2>
    </div>
    <div class="cardui-body">
      <div class="grid-container">
        <div v-for="item in items"
             :key="item.id"
             class="grid-item">
          <router-link :to="`/products/detail/${item.id}`"  class="item-link">
            <div class="image-container">
              <img :src="item.image" :alt="item.name">
            </div>
            <div class="item-title">{{ item.name }}</div>
          </router-link>
        </div>
      </div>
    </div>
    <div class="a-cardui-footer">
      <router-link :to="`/products/category?catId=${catId}&name=${title}`" class="a-link-normal">
        <span class="a-truncate">{{ footerText }}</span>
      </router-link>
    </div>
  </div>
</template>

<script setup>
defineProps({
  items: {
    type: Array,
    required: true
  },
  title: {
    type: String,
    required: true
  },
  catId: {
    type: String,
    required: true
  },
  footerText: {
    type: String,
    required: true
  }
})
</script>

<style lang="scss" scoped>
.a-cardui {
  background: #fff;
  padding: 20px;
  border-radius: 8px;
  box-shadow: 0 2px 5px rgba(15, 17, 17, 0.15);
  //height: 522px;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;

  @media screen and (max-width: 768px) {
    padding: 12px;
    border-radius: 4px;
    //height: 400px;
  }

  &-header {
    margin-bottom: 12px;

    .headline {
      font-size: 21px;
      line-height: 27.3px;
      font-weight: 700;
      color: #0F1111;

      @media screen and (max-width: 768px) {
        font-size: 18px;
        line-height: 24px;
      }
    }
  }
}

.cardui-body {
  flex: 1;
  margin-bottom: 12px;

  .grid-container {
    height: 100%;
    display: grid;
    grid-template-columns: repeat(2, 1fr);
    grid-template-rows: repeat(2, 1fr);
    gap: 12px;

    @media screen and (max-width: 768px) {
      gap: 8px;
    }
  }

  .grid-item {
    .item-link {
      height: 100%;
      text-decoration: none;
      color: inherit;
      display: flex;
      flex-direction: column;
    }

    .image-container {
      flex: 1;
      position: relative;
      margin-bottom: 8px;
      padding-top: 100%;
      position: relative;
      overflow: hidden;
      img {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        max-height: 147px;
        object-fit: cover;
        border-radius: 4px;
        // object-fit: contain;
      }
    }

    .item-title {
      font-size: 14px;
      color: #0F1111;
      text-align: center;
      width: 100%;

      @media screen and (max-width: 768px) {
        font-size: 12px;
      }
    }
  }
}

.a-cardui-footer {
  margin-top: auto;

  .a-link-normal {
    color: #007185;
    text-decoration: none;
    font-size: 14px;

    &:hover {
      color: #C7511F;
      text-decoration: underline;
    }

    @media screen and (max-width: 768px) {
      font-size: 13px;
    }
  }
}
</style>

/**
 * 四格卡片组件
 * 特点：
 * - 2x2网格布局
 * - 每个格子包含图片和标题
 * - 支持自定义标题和底部文字
 * - 所有项目可点击跳转
 * 主要用于展示分类入口，如家居必需品分类展示等
 */
